<?php
class ImportCsv{
    private $data = array();
    public function __construct() {
    }
    public function setData($data){
        if(!is_array($data))
            $this->setDataFromString($data);
        $this->data = $data;
    }
    
    private function setDataFromString(&$data){
        if (!function_exists('str_getcsv')) {
            $delimiter=';';
            $enclosure='"';
            //$escape=null;
            //$eol=null;
            $temp=fopen("php://memory", "rw");
            fwrite($temp, $data);
            fseek($temp, 0);
            $r = array();
            while (($tmp = fgetcsv($temp, 4096, $delimiter, $enclosure)) !== false) {
            $r = $tmp;
            }
            fclose($temp);
            $data = $r;
        }else{
            $data = str_getcsv($data, $delimiter=';', $enclosure='"');
        }
        if(count($data) < 0){
            throw new Exception('Chaine $data mal formattée dans ImportCsv::setDataFromString() ! > '.$data);
        }
    }
    private function toFloat(&$string){
        $string = preg_replace('#[^0-9\.,]#','',$string);
        $string = (float)preg_replace('#,#','.',$string);
    }
    public function getValue($key,$type='string'){
        if(isset($this->data[$key])){
            switch($type){
                case 'integer':
                    $this->data[$key] = preg_replace('#[^0-9\.,]#','',$this->data[$key]);
                    return (int)$this->data[$key];
                break;
                case 'float':
                    $this->toFloat($this->data[$key]);
                    return (float)$this->data[$key];
                break;
                case 'date':
                    if(preg_match('#^((?:19|20)[0-9]{2})[/\-]?([0-9]{2})[/\-]?([0-9]{2})#',$this->data[$key],$match)){
                        $date = sprintf('%s-%s-%s',$match[1],$match[2],$match[3]);
                    }elseif(preg_match('#^([0-9]{2})[/\-]?([0-9]{2})[/\-]?((?:19|20)[0-9]{2})#',$this->data[$key],$match)){
                        $date = sprintf('%s-%s-%s',$match[3],$match[2],$match[1]);
                    }elseif($this->data[$key] == ''){
                        $date = NULL;
                    }else{
                        $date = '2000-01-01';
                    }
                    return $date;
                break;
                case 'boolean':
                    if(preg_match('#yes|oui|true|1#',$this->data[$key])){
                        $this->data[$key] = 1;
                    }elseif(preg_match('#no|non|false|0#',$this->data[$key])){
                        $this->data[$key] = 0;
                    }else{
                        $this->data[$key] = 1;
                    }
                    return (int)$this->data[$key];
                break;
                case 'monnaie':
                    if(preg_match('#euros?#i',$this->data[$key])){
                        $this->toFloat($this->data[$key]);
                        $monnaie = preg_replace('#\.#',',',$this->data[$key]);
                    }elseif(preg_match('#cents?#i',$this->data[$key])){
                        $this->toFloat($this->data[$key]);
                        $monnaie = preg_replace('#\.#',',',($this->data[$key]/100));
                    }else{
                        $this->toFloat($this->data[$key]);
                        $monnaie = preg_replace('#\.#',',',$this->data[$key]/100);
                    }
                    return $monnaie;
                break;
                case 'MlMarque':
                    $this->data[$key] = trim($this->data[$key]);
                    $MlMarque = MlMarquesTable::getInstance()->findByName($this->data[$key]);
                    if($MlMarque->getLast()){
                        $marque_id = $MlMarque->getLast()->getId();
                    }else{
                        if($this->data[$key] == ''){
                            return false;
                        }
                        $marque = new MlMarques();
                        $marque->setName($this->data[$key]);
                        $marque->save();
                        $marque_id = $marque->getId();
                    }
                    return (int)$marque_id;
                break;
                case 'MlDenomination':
                    $this->toFloat($this->data[$key]);
                    $MlDeno = MlDenominationsTable::getInstance()->findDeno((float)$this->data[$key]);
                    if($MlDeno->getLast()){
                        $denomination = array((int)$MlDeno->getLast()->getId());
                    }else{
                        if($this->data[$key] == 0){
                            return false;
                        }
                        $deno = new MlDenominations();
                        $deno->setName((float)$this->data[$key]);
                        $deno->save();
                        $denomination = array((int)$deno->getId());
                    }
                    return $denomination;
                break;
                case 'string':
                default:
                    return $this->data[$key];
            }
        }else{
            return false;
        }
    }
    public function getRaw(){
        return $this->data;
    }
}
?>
